/*
 * Табличный вид
 */

package mops.view;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumnBuilder;
import javafx.scene.control.TableView;
import javafx.scene.control.TableViewBuilder;

import javafx.scene.control.cell.PropertyValueFactory;
import mops.commons.Commons;
import mops.view.model.Person;

/**
 *
 * @author Жук Александр
 */
public class GroupObjectTable {
    
    private final TableView mainTable;
    
    public GroupObjectTable (){
       mainTable = createTable();
    }
    
 private  TableView createTable() {
    TableView table = TableViewBuilder.create()
    .columns(
        TableColumnBuilder.create()
        .text("First Name")
        .cellValueFactory(new PropertyValueFactory("firstName"))
        .prefWidth(180)
        .build(),
        TableColumnBuilder.create()
        .text("Last Name")
        .cellValueFactory(new PropertyValueFactory("lastName"))
        .prefWidth(180)
        .build(),
        TableColumnBuilder.create()
        .text("Phone Number")
        .cellValueFactory(new PropertyValueFactory("phone"))
        .prefWidth(150)
        .build()
    )
    .items(getTeamMembers())
    .build();

    table.getSelectionModel().selectedItemProperty()
        .addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                Person selectedPerson = (Person)newValue;
                System.out.println(selectedPerson + " chosen in TableView");
            }
        });
    return table;
} 
   
    private ObservableList getTeamMembers() {
        ObservableList teamMembers = FXCollections.observableArrayList();
        for (int i = 1; i <= 100; i++) {
            teamMembers.add(new Person("FirstName" + i,
                "LastName" + i,
                "Phone" + i));
        }
    return teamMembers;
}
    
    //Получение главного меню    
    public TableView getTable() throws Exception {
        if (mainTable!=null){
          return mainTable;  
        }
        else
        {
          throw new Exception(Commons.GET_TABLE_ERROR);  
        }                
    }
    
}
